<template>
	<el-card>
		<div slot="header" class="clearfix">
			<!--工具条-->
			<el-form :inline="true" :model="filters" class="filter-form-box">
				<el-form-item>
					<el-input size="small" v-model="filters.content" class="input-with-select" placeholder="请输入你要查询的内容">
						<el-select v-model="filters.select" slot="prepend" placeholder="请选择" style="width: 75px;">
							<el-option label="工号" value="1"></el-option>
							<el-option label="名称" value="2"></el-option>
						</el-select>
					</el-input>
				</el-form-item>
				<el-form-item style="margin-right: 0;">
					<el-button size="mini" v-on:click="isShowAdvancedSearch=!isShowAdvancedSearch" title="高级搜索">
						<i class="el-icon-plus" v-show="!isShowAdvancedSearch"></i>
						<i class="el-icon-minus" v-show="isShowAdvancedSearch"></i>
					</el-button>
				</el-form-item>
				<el-form-item style="margin-right: 0;">
					<el-button size="mini" v-on:click="refreshFilters" title="重置"><i class="fa fa-refresh"></i></el-button>
				</el-form-item>
				<el-form-item>
					<el-button size="mini" v-on:click="getData" title="搜索"><i class="fa fa-search"></i></el-button>
				</el-form-item>
				<el-form-item style="float: right">
					<el-button type="primary" size="small" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
			<el-form v-show='isShowAdvancedSearch' :inline="true" :model="filters" class="filter-form-box advanced-search-form">
				<el-form-item>
					<el-select size="small" v-model="filters.departmentId" placeholder="请选择院系">
						<el-option
								v-for="item in departmentList"
								:key="item.id"
								:label="item.name"
								:value="item.id">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-date-picker
							size="small"
							v-model="filters.entranceYear"
							type="year"
							value-format="yyyy"
							placeholder="选择入职年份">
					</el-date-picker>
				</el-form-item>
			</el-form>
		</div>

		<!--列表-->
		<el-table border stripe :data="datas" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="schoolNumber" label="工号" sortable>
			</el-table-column>
			<el-table-column prop="name" label="姓名" sortable>
			</el-table-column>
			<el-table-column prop="departmentName" label="所属院系" sortable>
			</el-table-column>
			<el-table-column prop="entranceYear" label="入职年份" sortable>
			</el-table-column>
			<el-table-column prop="duty" label="职称" sortable>
			</el-table-column>
			<el-table-column prop="remark" label="备注" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template slot-scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="20" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增/编辑界面-->
		<el-dialog title="新增" :visible.sync="formVisible" :close-on-click-modal="false">
			<el-form :model="formData" label-width="80px" :rules="formRules" ref="formData">
				<el-form-item label="工号" prop="schoolNumber">
					<el-input v-model="formData.schoolNumber" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="姓名" prop="name">
					<el-input v-model="formData.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="所属院系" prop="departmentId">
					<el-select  v-model="formData.departmentId" placeholder="请选择">
						<el-option
								v-for="item in departmentList"
								:key="item.id"
								:label="item.name"
								:value="item.id">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="入职日期" prop="entranceYear">
					<el-date-picker
							v-model="formData.entranceYear"
							type="year"
							value-format="yyyy"
							placeholder="选择入职年份">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="职称" prop="duty">
					<el-input v-model="formData.duty" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="备注">
					<el-input type="textarea" v-model="formData.remark"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button size="small" @click.native="formVisible = false">取消</el-button>
				<el-button type="primary" size="small" @click.native="saveSubmit(1)" :loading="saveLoading" v-show="formType==='add'">新增</el-button>
				<el-button type="primary" size="small" @click.native="saveSubmit(2)" :loading="saveLoading" v-show="formType==='edit'">保存</el-button>
			</div>
		</el-dialog>
	</el-card>
</template>

<script>
  import {getDepartmentList, getTeacherList, removeUserData, saveUserData, upDateUserData } from '../../api/api';

  export default {
    data() {
      return {
        filters: {
          select: '1',
          departmentId:'',
          content: '',
          entranceYear: '',
        },
        isShowAdvancedSearch: '',
        datas: [],
        departmentList: [],
        total: 0,
        page: 0,
        listLoading: false,
        sels: [],//列表选中列
        formType:'',
        formVisible: false,//新增界面是否显示
        saveLoading: false,
        formRules: {
          departmentId: [
            { required: true, message: '请选择所属院系', trigger: 'blur' }
          ],
          entranceYear: [
            { required: true, message: '请选择入职年份', trigger: 'blur' }
          ],
          schoolNumber: [
            { required: true, message: '请输入教师工号', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '请输入教师姓名', trigger: 'blur' }
          ],
          duty: [
            { required: true, message: '请输入教师职称', trigger: 'blur' }
          ]
        },
        //新增界面数据
        formData: {
          entranceYear: '',
          schoolNumber: '',
          departmentName: '',
          departmentId:'',
          name: '',
          duty: '',
          remark: ''
        }
      }
    },
    methods: {
      handleCurrentChange(val) {
        this.page = val;
        this.getData();
      },

	  //重置搜索条件
      refreshFilters(){
        this.filters = {
          select: '1',
          departmentId:'',
          content: '',
          entranceYear: '',
        };
        this.getData();
      },

      //获取数据列表
      getData() {
        let para = {
          count: 10,
          pageNo: this.page,
          sortDirection: 'asc',
          sortField: ["name"],
		  classId: '',
          specialtyId: '',
          name: '',
          schoolNumber: '',
          departmentId: this.filters.departmentId,
          entranceYear: this.filters.entranceYear,
        };

        if(this.filters.select === '1'){
          para.schoolNumber = this.filters.content;
        }else if(this.filters.select === '2'){
          para.name = this.filters.content;
        }

        this.listLoading = true;
        getTeacherList(para).then((res) => {
          this.total = res.data.data.totalPages;
          this.datas = res.data.data.content;
          this.listLoading = false;
        });
      },


      //获取院系数据
      getDepartmentData(){
        let para = {
          count: 100000,
          pageNo: 0,
          sortDirection: 'asc',
          sortField: ["name"],
          name: '',
        };
        getDepartmentList(para).then((res) => {
          this.departmentList = res.data.data.content;
        });
      },

      //显示编辑界面
      handleEdit: function (index, row) {
        this.formType = 'edit';
        this.formVisible = true;
        this.formData = Object.assign({}, row);
      },

      //显示新增界面
      handleAdd: function () {
        this.formType = 'add';
        this.formVisible = true;
        this.formData = {
          entranceYear: '',
          schoolNumber: '',
          departmentName: '',
          departmentId:'',
          name: '',
          duty: '',
          remark: ''
        };
      },

      //新增
      saveSubmit: function (num) {
        this.$refs.formData.validate((valid) => {
          if (valid) {
            let user = sessionStorage.getItem('user');
            if (user) {
              user = JSON.parse(user);
              let T_msg = '';
              let jsonStr = [];
              if(num === 1){
                jsonStr = {
                  entranceYear: '',
                  schoolNumber: '',
                  departmentName: '',
                  departmentId:'',
                  authorUserId: '',
                  specialtyId: '',
                  classID: '',
                  name: '',
                  icon: '',
                  duty: '',
                  remark: '',
                  roleType: 1
                };

                jsonStr.duty = this.formData.duty;
                jsonStr.schoolNumber = this.formData.schoolNumber;
                jsonStr.entranceYear = this.formData.entranceYear;
                jsonStr.departmentName = this.departmentList.find(item => item.id === this.formData.departmentId)['name'];
                jsonStr.departmentId = this.formData.departmentId;
                jsonStr.name = this.formData.name;
                jsonStr.remark = this.formData.remark;
                jsonStr.authorUserId = user.id;
                this.$confirm('确认添加吗？', '提示', {}).then(() => {
                  this.saveLoading = true;
                  this.saveLoading = false;
                  saveUserData(jsonStr).then((res) => {
                    this.saveLoading = false;
                    this.$message({
                      message: '提交成功',
                      type: 'success'
                    });
                    this.$refs['formData'].resetFields();
                    this.formVisible = false;
                    this.refreshFilters();
                  });
                });
              }else if(num === 2){
                jsonStr = Object.assign({}, this.formData);
                jsonStr.departmentName = this.departmentList.find(item => item.id === this.formData.departmentId)['name'];
                this.$confirm('确认修改吗？', '提示', {}).then(() => {
                  this.saveLoading = true;
                  this.saveLoading = false;
                  upDateUserData(jsonStr).then((res) => {
                    this.saveLoading = false;
                    this.$message({
                      message: '提交成功',
                      type: 'success'
                    });
                    this.$refs['formData'].resetFields();
                    this.formVisible = false;
                    this.refreshFilters();
                  });
                });
              }

            }
          }
        });
      },

      selsChange: function (sels) {
        this.sels = sels;
      },
    },
    mounted() {
      this.getData();
      this.getDepartmentData();
    }
  }

</script>

<style scoped>

</style>